package com.android.locationapi.maps;

import java.util.ArrayList;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import com.google.android.maps.GeoPoint;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class LocationSender extends Activity implements Runnable{

	private String userName;
	private LocationManager locationManager;
	private Location lastKnownLocation;
	private Location previousLocation; 
	
	LocationSender(String userName)
	{
		this.userName = userName;
		this.previousLocation = null;
		this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0, new GeoUpdateHandler() );
	}
	
	@Override
	public void run() {	
		while(true)
		{
			lastKnownLocation = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
			if(previousLocation!=null)
			{
				//Check if the new location is different from the previous location
				if(previousLocation.getLatitude()!=lastKnownLocation.getLatitude()
						||previousLocation.getLongitude()!=lastKnownLocation.getLongitude())
				{
					//Send it
					 double latitude = lastKnownLocation.getLatitude();
				     double longitude = lastKnownLocation.getLongitude();
				     
				     //http post
			          try{
			        	  ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
			        	  data.add(new BasicNameValuePair("username", this.userName));
			        	  data.add(new BasicNameValuePair("latitude", Double.toString(latitude)));
			        	  data.add(new BasicNameValuePair("longitude", Double.toString(longitude)));
			        	  Log.v("Location Sender", "Sent " + this.userName);
			        	  Log.v("Location Sender", "Sent " + latitude);
			        	  Log.v("Location Sender", "Sent " + longitude);
			        	  HttpClient httpclient = new DefaultHttpClient();
			              HttpPost httppost = new HttpPost(serveripaddress.servipaddr+"login.php");
			              httppost.setEntity(new UrlEncodedFormEntity(data));
			              Log.v("Server - Location Update", "Here now");
			              httpclient.execute(httppost);
			          }
			          catch(Exception e){
			                  Log.v("log_tag", "Error in http connection "+e.toString());
			          }
				}
				previousLocation = lastKnownLocation;
			}
		try {
			Thread.sleep(30000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		}	
	}
public class GeoUpdateHandler implements LocationListener {
		
		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat, lng);		
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub
			
		}
}
}
